package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.Order;
import com.yami.shop.bean.model.UserAddrOrder;
import com.yami.shop.dao.OrderMapper;
import com.yami.shop.dao.UserAddrOrderMapper;
import com.yami.shop.service.UserAddrOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @author yami
 */
@Service
public class UserAddrOrderServiceImpl extends ServiceImpl<UserAddrOrderMapper, UserAddrOrder> implements UserAddrOrderService{

    @Autowired
    private OrderMapper orderMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAddrInfoById(UserAddrOrder userAddrOrder, Order orderDb) {
        boolean notModifyAddress = Objects.isNull(userAddrOrder.getAreaId()) || Objects.isNull(userAddrOrder.getCityId()) || Objects.isNull(userAddrOrder.getProvinceId());
        // 没有修改地址, 只需要修改收货人和手机号
        if (notModifyAddress) {
            userAddrOrder = new UserAddrOrder();
            userAddrOrder.setAddrOrderId(userAddrOrder.getAddrOrderId());
            userAddrOrder.setMobile(userAddrOrder.getMobile());
            userAddrOrder.setReceiver(userAddrOrder.getReceiver());
        }
        updateById(userAddrOrder);
        // 修改订单相关信息
        orderDb.setReceiverMobile(userAddrOrder.getMobile());
        orderDb.setReceiverName(userAddrOrder.getReceiver());
        orderMapper.updateById(orderDb);
    }
}
